<?php

/**
 * Desc: 获取积分控制器
 * Created by chenxinxing
 * Date: 2018-08-15
 */
namespace App\Http\WechatAuth\Controllers;

use App\Api\BaseClass\Controller;
use App\Http\WechatAuth\Models\User;
use App\Http\WechatAuth\Models\UserIntegralLog;
use App\Http\WechatAuth\Services\AuthContract;
use App\Core\Facades\User as UserFacades;
use App\Http\ManagerApi\Models\PlatformSetting;
use App\Http\WechatAuth\Models\IntegraLog;
use Illuminate\Support\Facades\DB;
use App\Http\WechatAuth\Models\TeamMember;

class GetIntegralController extends Controller
{

    public function __contruct(AuthContract $auth)
    {
        parent::__construct();
    }

    /**
     * 获取当前积分和总积分
     */
    public function getIntegral()
    {

        $user = UserFacades::user();
        // 总收益
        $return['totalIntegral'] = $user->total_integral;
        // 可用积分
        $return['integral'] = $user->integral;
        // 总支出
        $return['zhichu'] = IntegraLog::where('uid', $user->id)
            ->where('flag', 0)
            ->where('is_finished', 1)
            ->sum('changed_integral');
        // 总收入
        $return['shouru'] = IntegraLog::where('uid', $user->id)
            ->where('flag', 1)
            ->where('is_finished', 1)
            ->sum('changed_integral');
        // 金豆总转出
        $return['totalSend'] = IntegraLog::where('uid', $user->id)
            ->where('flag', 0)
            ->where('type', 3)
            ->where('is_finished', 1)
            ->sum('changed_integral');
        // 金豆总收入
        $return['totalRecieve'] = IntegraLog::where('uid', $user->id)
            ->where('flag', 1)
            ->where('type', 3)
            ->where('is_finished', 1)
            ->sum('changed_integral');

        return $this->form->datas($return);
    }

    /**
     * 获取账户详情
     */
    public function getIntegralDetail()
    {

        $rules = [
            ['offset', 'required|integer'],
            ['limit', 'required|integer']
        ];
        $params = $this->form->camelFormOrFail($rules);
        $search_params = [
            ['orderBy', 'created_at', 'desc'],
            ['where', 'uid', UserFacades::user()->id],
        ];
        $UserIntegralLog = (new UserIntegralLog)->pagination($params, $search_params);
        return $this->form->getMessage($UserIntegralLog);
    }

    /**
     * 获取平台配置
     */
    public function getPlatformSetting()
    {
        return $this->form->getMessage(PlatformSetting::firstOrFail());
    }

    public function test()
    {
        DB::transaction(function () {
            $users = User::all();
            foreach ($users as $user) {
                $subs = $this->getSubs($user->id);
                $user->total_achievement = $subs * 10000;
                $user->save();
            }
        });
    }

    public function getSubs($uid)
    {
        $all = TeamMember::where('uid', $uid)->get();
        $subs = 0;
        $subs += count($all);
        foreach ($all as $val) {
            $is_exists = User::find($val->sub_uid);
            if (!$is_exists) {
                $subs -= 1;
                continue;
            }

            $subs += $this->getSubs($val->sub_uid);
        }

        return $subs;
    }
}


